import { dateFormat } from '@/utils/string'
import qs from 'qs'

let userList = [
  {
    userId: '001',
    userName: 'admin',
    userAlias: '管理员',
    userPassword: '111111',
    userAvatar: '',
    userStatus: 1,
    userSex: 1,
    userPhone: '13660270951',
    userEmail: '13660270951@163.com',
    createTime: '2020-05-20',
    userDesc: '这家伙很懒什么也没有留下',
    roles: ['ADMIN'],
    roleNames: ['超级管理员'],
    token: 'f2a986c910e040e4891dd67a57d6136d'
  },
  {
    userId: '002',
    userName: 'bangzz',
    userAlias: '周大侠',
    userPassword: '111111',
    userAvatar: '',
    userStatus: 1,
    userSex: 1,
    userPhone: '13660270951',
    userEmail: '448341949@qq.com',
    createTime: '2020-05-20',
    userDesc: '呼呼哈嘿，快使用双截棍！',
    roles: ['MANAGER', 'USER'],
    roleNames: ['管理员', '普通用户'],
    token: 'd2a45245828e4fb8aafd7337c2464971'
  },
  {
    userId: '003',
    userName: 'user1',
    userAlias: '用户甲',
    userPassword: '111111',
    userAvatar: '',
    userStatus: 1,
    userSex: 0,
    userPhone: '13117536320',
    userEmail: '448341949@qq.com',
    createTime: '2020-05-21',
    userDesc: '路人甲......',
    roles: ['USER'],
    roleNames: ['普通用户'],
    token: '74470154c77a4c90a7536438392f5c17'
  },
  {
    userId: '004',
    userName: 'user2',
    userAlias: '用户乙',
    userPassword: '111111',
    userAvatar: '',
    userStatus: 0,
    userSex: 0,
    userPhone: '13117536320',
    userEmail: '1205339567@qq.com',
    createTime: '2020-05-22',
    userDesc: '路人乙......',
    roles: ['USER'],
    roleNames: ['普通用户'],
    token: 'b09c283d274548a1b6d967a5ad211cf2'
  }
]

export default {
  'get|/sys/user/list': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const params = url.split('?')[url.split('?').length - 1]
    const data = qs.parse(params)
    // mock数据不做分页
    // const page = data.page
    // const limit = data.limit
    const name = data.name
    // mock数据不做类型查询
    // const type = data.type
    const newList = []
    userList.forEach(item => {
      // 过滤-超级管理员角色的用户
      if (item.roles.indexOf('ADMIN') > -1) {
        return true
      }
      const obj = {
        'userId': item.userId,
        'userName': item.userName,
        'userAlias': item.userAlias,
        'userStatus': item.userStatus,
        'userSex': item.userSex,
        'userPhone': item.userPhone,
        'userEmail': item.userEmail,
        'createTime': item.createTime,
        'userDesc': item.userDesc,
        'roleNames': item.roleNames
      }
      if (name !== '') {
        if (obj.userName.indexOf(name) > -1 || obj.userAlias.indexOf(name) > -1) {
          newList.push(obj)
          return true
        }
      } else {
        newList.push(obj)
      }
    })
    ret.data = {
      totalCount: userList.length - 1,
      dataList: newList
    }
    return ret
  },
  'get|/sys/user/info/': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const userId = url.split('/')[url.split('/').length - 1]
    const user = userList.find(item => {
      return item.userId === userId
    })
    if (user) {
      const newUser = {
        userId: user.userId,
        userName: user.userName,
        userAlias: user.userAlias,
        userSex: user.userSex,
        userPhone: user.userPhone,
        userEmail: user.userEmail,
        userDesc: user.userDesc,
        roleNames: user.roleNames
      }
      ret.data = newUser
    } else {
      ret.code = 500
      ret.msg = '无此id[' + userId + ']的用户'
    }
    return ret
  },
  'post|/sys/user/info': option => {
    let ret = {code: 200, success: false, msg: '', data: null}
    let user = JSON.parse(option.body)
    const exist = userList.some(item => {
      return item.userName === user.userName
    })
    if (!exist) {
      user['userId'] = Math.floor((Math.random() * 100) + 1) + ''
      user['userStatus'] = 1
      user['userAvatar'] = ''
      user['roles'] = ['USER']
      user['roleNames'] = ['普通用户']
      user['createTime'] = dateFormat(new Date(), 'YYYY-MM-DD')
      user['token'] = 'test'
      userList.push(user)
      ret.msg = '用户成功！请关闭mockjs开启后台体验完整功能！'
    } else {
      ret.code = 500
      ret.msg = '用户失败，该登录账号名已存在！'
    }
    return ret
  },
  'put|/sys/user/info': option => {
    let ret = {code: 200, success: false, msg: '', data: null}
    const user = JSON.parse(option.body)
    let isUpdate = userList.some(item => {
      if (item.userId === user.userId) {
        item.userAlias = user.userAlias
        item.userSex = user.userSex
        item.userPhone = user.userPhone
        item.userEmail = user.userEmail
        item.userDesc = user.userDesc
        return true
      }
    })
    if (isUpdate) {
      ret.msg = '用户成功！请关闭mockjs开启后台体验完整功能！'
    } else {
      ret.code = 500
      ret.msg = '用户失败,找不到此用户！'
    }
    return ret
  },
  'put|/sys/user/status': option => {
    let ret = {code: 200, msg: '', data: null}
    const url = option.url
    const userId = url.split('/')[url.split('/').length - 1]
    const data = JSON.parse(option.body)
    const userStatus = data.status
    const isChanged = userList.some(item => {
      if (item.userId === userId) {
        item.userStatus = userStatus
        return true
      }
    })
    if (isChanged) {
      ret.msg = '用户成功！'
    } else {
      ret.code = 500
      ret.msg = '用户失败！'
    }
    return ret
  },
  'post|/sys/user/delBatch': option => {
    let ret = {code: 0, success: false, msg: '', data: null}
    const data = JSON.parse(option.body)
    userList.forEach((item) => {
      if (data.ids.indexOf(item.userId) > -1) {
        item.userStatus = 0
      }
    })
    ret.success = true
    ret.msg = '成功停用' + data.ids.length + '个用户！'
    return ret
  }

}
